package com.chang.bootplus.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chang.bootplus.base.annotation.PublicVisit;
import com.chang.bootplus.base.exception.BusException;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.Bmi;
import com.chang.bootplus.entity.School;
import com.chang.bootplus.entity.StuTest;
import com.chang.bootplus.entity.Student;
import com.chang.bootplus.po.*;
import com.chang.bootplus.service.SchoolService;
import com.chang.bootplus.service.StuTestService;
import com.chang.bootplus.service.StudentService;
import com.chang.bootplus.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 * aSBABDHJSAJD
 *
 * @author chang
 * @since 2022-01-23
 */
@Api(tags = {"学校信息及管理"})
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SchoolController {

    final SchoolService schoolService;
    final StudentService studentService;
    final StuTestService stuTestService;

    @PublicVisit
    @PostMapping("/school/register")
    @ApiOperation(httpMethod = "POST", value = "学校注册")
    public ApiResponse<Void> register(@RequestBody SchoolPO po){
        return schoolService.register(po);
    }

    @PublicVisit
    @PostMapping("/addSchool")
    @ApiOperation(httpMethod = "POST", value = "添加学校")
    public ApiResponse<Void> addSchool(@RequestBody SchoolNamePO po){
        if(schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getSchoolName,po.getSchoolName())) != null){
            throw new BusException("已经有该学校了");
        }
        School school = BeanUtil.beanA2beanB(po, School.class);
        boolean save = schoolService.save(school);
        if(!save){
            throw new BusException("添加失败");
        }

        return new ApiResponse().setReMsg("添加成功");
    }

    @PublicVisit
    @PostMapping("/schoolLogin")
    @ApiOperation(httpMethod = "POST", value = "学校登录")
    public ApiResponse<Void> login(@RequestBody SchoolLoginPO po){
        return new ApiResponse();
    }


    @PostMapping("/logout")
    @ApiOperation(httpMethod = "POST", value = "学校退出登录")
    public ApiResponse<Void> logout(){
        return new ApiResponse();
    }

    @PostMapping("/schoolRename")
    @ApiOperation(httpMethod = "POST", value = "学校编辑")
    public ApiResponse<Void> reName(@RequestBody SchoolRanamePO po){

        School byId = schoolService.getById(po.getId());
        // 原来的学校名
        String schoolName = byId.getSchoolName();
        byId.setSchoolName(po.getSchoolName());
        // dasa
        boolean b = schoolService.updateById(byId);
        if(!b){
            throw new BusException("更新失败");
        }
        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getSchoolName,schoolName));
        list.stream().forEach(t->{
            t.setSchoolName(byId.getSchoolName());
            studentService.updateById(t);
        });

        return new ApiResponse().setReMsg("更新成功");
    }


    @PublicVisit
    @PostMapping("/deleteschool")
    @ApiOperation(httpMethod = "POST", value = "删除学校")
    public ApiResponse<Void> deleteschool(@RequestBody SchoolIdPO po){
        School byId = schoolService.getById(po.getId());
        Assert.notNull(byId, "学校不存在");
        schoolService.removeById(byId);

        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getSchoolName, byId.getSchoolName()));

        studentService.remove(new LambdaQueryWrapper<Student>().eq(Student::getSchoolName,byId.getSchoolName()));

        //删除按照学号和学校删除
        list.stream().forEach(student -> {
            stuTestService.remove(new LambdaQueryWrapper<StuTest>().eq(StuTest::getSchoolName,byId.getSchoolName()).eq(StuTest::getStuId,student.getStuId()));
        });

        return new ApiResponse("删除成功");
    }

    @PostMapping("/schoolMessageDisplay")
    @ApiOperation(httpMethod = "POST", value = "学校首页信息展示")
    public ApiResponse<SchoolMessageVO> messageDisplay(@RequestBody SchoolNeedPO po){
        return schoolService.messageDisplay(po);
    }




    // 分页
    @PostMapping("/schoolManage")
    @ApiOperation(httpMethod = "POST", value = "后台学校管理列表展示 (不传学校名称则为展示所有的学校列表，" +
            "传学校名称则为模糊查询展示对应的学校列表) ")
    public ApiResponse<SchoolBackMessageVO> schoolManage(@RequestBody SchoolBackNamePO po){
        return schoolService.schoolManage(po);
    }

    @PostMapping("/countCal")
    @ApiOperation(httpMethod = "POST", value = "后台学校管理工作台的数据统计")
    public ApiResponse<CountCalVO> countCal(){
        return schoolService.countCal();
    }

    @PostMapping("/workbenchDetail")
    @ApiOperation(httpMethod = "POST", value = "后台工作台显示的成绩柱状图和四个率")
    public ApiResponse<WorkbenchDetailsVO> workbenchDetail(@RequestBody SchoolNamePO po){
        return schoolService.workbenchDetail(po);
    }

    @PostMapping("/schoolTestTime")
    @ApiOperation(httpMethod = "POST", value = "拿到学校的各个测试时间")
    public ApiResponse<SchoolTestTimeVO> testTimeDisplay(@RequestBody SchoolNamePO po){
        return schoolService.testTimeDisplay(po);
    }

    @PostMapping("/schoolGrade")
    @ApiOperation(httpMethod = "POST", value = "拿到学校共有哪些年级")
    public ApiResponse<SchoolGradeVO> testGradeDisPlay(@RequestBody SchoolNamePO po){
        return schoolService.testGradeDisPlay(po);
    }

    @PostMapping("/schoolClass")
    @ApiOperation(httpMethod = "POST", value = "拿到学校某个年级共有哪些班级")
    public ApiResponse<SchoolClassVO> testClassDisplay(@RequestBody SchoolNamePO po){
        return schoolService.testClassDisplay(po);
    }


}

